//
//  ComplainController.m
//  PropertyMananger
//
//  Created by Mansion on 17/1/16.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "ComplainController.h"
#import "ComplainCell.h"
#import "ComplainDetailController.h"
#import "ComplainModel.h"

@interface ComplainController ()
@property (nonatomic,strong) NSMutableArray * complainArray;//投诉数据源

@end

@implementation ComplainController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleViewName:@"投诉单"];
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH-64) isGroup:NO];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    
    self.tableView.backgroundColor = BHColor(238, 238, 238);
    
    self.complainArray = [[NSMutableArray alloc]init];
    [self createRefresh];
    [self createPlaceHolder];
    
    [self postComplainData];
}
#pragma mark - 创建刷新UI
- (void)createRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPage = 1;
        
        self.isDown = YES;
        //获取数据
         [self postComplainData];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.nextPage += 1;
        
        self.isDown = NO;
        //获取数据
        [self postComplainData];
        
        //    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.complainArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    ComplainCell * cell = [ComplainCell cellWithTableView:tableView];
    ComplainModel * model = self.complainArray[indexPath.section];
    
    cell.model = model;
//    self.titleStr = @"投诉内容:";
//    self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,model.Contents];
//    
//    self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
//    [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
//    cell.contentLabel.attributedText = self.stra;
    
    cell.complianManLabel.text = model.ComplaintMan;
    cell.complainCodeLabel.text = [NSString stringWithFormat:@"投诉编号:%@",model.ComplaintCode];
    cell.timeLabel.text = model.ComplaintTime;
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 210;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 0.001f;
    }else{
    
        return 10;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UITableViewHeaderFooterView * headerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"header"];
    
    return headerView;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ComplainDetailController * complainDetailVC = [[ComplainDetailController alloc]init];
    complainDetailVC.model = self.complainArray[indexPath.section];
    [self.navigationController pushViewController:complainDetailVC animated:YES];
}
#pragma mark - 获取投诉列表数据
- (void)postComplainData
{
    NSDictionary * parms = nil;
    parms = @{
              @"CommunityID":userModel.CommunityID,
              @"Type":@"10",
              @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"complaintlist\",\"version\":\"V1.0\"}"
              };
    
    [WorkOrderRequest postComplainSuccess:^(id data) {
        
        if (self.isDown == YES) {
            
            [self.complainArray removeAllObjects];
            self.isDown = NO;
        }
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
          DLog(@"投诉列表的数据 == %@",array);
        for (NSDictionary * dict  in array) {
            
            ComplainModel * model = [[ComplainModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.complainArray addObject:model];
            
        }
        if (self.complainArray.count != 0){
            
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
            
        }else{
            
            self.tableView.mj_footer.hidden = YES;
            
            self.placehoderImg.hidden = NO;
            self.titleLabel.hidden = NO;
        }
        
        if (self.complainArray.count <= 3) {
            
            self.tableView.mj_footer.hidden = YES;
        }


        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } param:parms];

}
#pragma mark - 暂未图
- (void)createPlaceHolder
{
    self.placehoderImg = [[UIImageView alloc]init];
    [self.tableView addSubview:self.placehoderImg];
    self.placehoderImg.image = [UIImage imageNamed:@"ico_complaint_no"];
    
    [self.placehoderImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.tableView.mas_bottom).with.offset(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.tableView addSubview:self.titleLabel];
    self.titleLabel.text = @"暂无投诉单";
    
    self.titleLabel.textColor= BHColor(153, 153, 153);
    self.titleLabel.font = BHFont(15);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.placehoderImg.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(100, 25));
    }];
    
}

@end
